#include "hardware_driver.h"
#include "stm8s_clk.h"
#include "timeout.h"
#include "adcScan.h"
#include "batteryStatusKey.h"


static int Uart_Init(uint32_t BaudRate){
	UART1_Init((uint32_t)BaudRate, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, 
		UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
	UART1_ITConfig(UART1_IT_RXNE_OR,ENABLE);
	UART1_Cmd(ENABLE);
}

static void Tim4_Init(void){
	#define PERIOD_VALUE 		125
	TIM4_TimeBaseInit( TIM4_PRESCALER_64,PERIOD_VALUE - 1);
	TIM4_PrescalerConfig(TIM4_PRESCALER_64,TIM4_PSCRELOADMODE_IMMEDIATE);
	TIM4_ARRPreloadConfig(ENABLE);
	TIM4_ITConfig( TIM4_IT_UPDATE , ENABLE);
	TIM4_UpdateRequestConfig( TIM4_UPDATESOURCE_GLOBAL);
	TIM4_Cmd(ENABLE );
}

/*****************************************************************************
 * Function      : system_init
 * Description   : system init 
 * Input         : void  
 * Output        : None
 * Return        : 
 * Others        : 
 * Record
 * 1.Date        : 20170321
 *   Author      : bruce
 *   Modification: Created function

*****************************************************************************/
int system_init(void)
{
	/* init system clock must be fixed 8Mhz*/
	CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV2);
	/* GPIO port init*/
	ledArray_GpioInit();
	/* enable usart serial peripheral and interrupt*/
	//Uart_Init(UART_BAUDRATE_VALUE);
	/* adc channel init */
	adcSan_DeInit();
	batteryStatusKey_init();
	/* timer init */
 	Tim4_Init();
	TimeOutDet_Init();
	return 0;
}



